package com.yingxin.yxerp.module.inform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yingxin.yx.framework.commons.enums.BusinessType;
import com.yingxin.yxerp.module.inform.entity.NoticeType;
import com.yingxin.yxerp.module.inform.mapper.NoticeTypeMapper;
import com.yingxin.yxerp.module.inform.service.INoticeTypeService;
import com.yingxin.yxerp.module.inform.vo.req.NoticeTypeQueryVo;
import com.yingxin.yxerp.module.inform.vo.resp.NoticeTypeRespVo;
import com.yingxin.yxerp.module.oplog.aop.OplogAnnotation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 通知公告-公告类型 服务实现类
 * @author zhuchangjie
 * @since 2021-12-21
 */

@Service
public class NoticeTypeServiceImpl extends ServiceImpl<NoticeTypeMapper, NoticeType> implements INoticeTypeService {

    /**
     * 分页查询
     */
    @Override
    public IPage<NoticeTypeRespVo> pageVo(Page page, NoticeTypeQueryVo queryVo) {
        IPage<NoticeTypeRespVo> voPage = baseMapper.pageVo(page, queryVo);
        return voPage;
    }

    /**
     * 查询全部
     */
    @Override
    public List<NoticeTypeRespVo> listVo(NoticeTypeQueryVo queryVo) {
        List<NoticeTypeRespVo> voList = baseMapper.listVo(queryVo);
        return voList;
    }

    /**
     * 根据id查询
     */
    @Override
    public NoticeTypeRespVo getVo(String id) {
        NoticeTypeRespVo vo = baseMapper.getVo(id);
        return vo;
    }

    /**
     * 创建
     */
    @OplogAnnotation(biz = BusinessType.TZGGLX, id = "{{noticeType.id}}", action = "新增", desc = "名称：{{noticeType.typeName}}")
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void create(NoticeType noticeType) {
        save(noticeType);
    }

    /**
     * 修改
     */
    @OplogAnnotation(biz = BusinessType.TZGGLX, id = "{{noticeType.id}}", action = "修改", desc = "名称：{{noticeType.typeName}}")
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void update(NoticeType noticeType) {
        updateById(noticeType);
    }

    /**
     * 根据id删除
     */
    @OplogAnnotation(biz = BusinessType.TZGGLX, id = "{{noticeType.id}}", action = "删除", desc = "名称：{{noticeType.typeName}}")
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void delete(NoticeType noticeType) {
        removeById(noticeType);
    }

    /**
     * 查询count
     */
    @Override
    public Integer countByTypeNameNeId(String typeName, String id) {
        QueryWrapper<NoticeType> qw = new QueryWrapper<>();
        qw.lambda().eq(NoticeType::getTypeName, typeName);
        if (StringUtils.isNotBlank(id)) {
            qw.lambda().ne(NoticeType::getId, id);
        }
        return count(qw);
    }

}
